using System;
using System.Collections.Generic;
using Matrix.Notification.Domain.Shared.Notifications;
using Matrix.Notification.Application.Contracts.Dto;

namespace Matrix.Notification.Application.Notifications.Dto
{
    /// <summary>
    /// 获取通知统计输入参数
    /// </summary>
    public class GetNotificationStatisticsInput
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 通知类型过滤
        /// </summary>
        public NotificationType? Type { get; set; }

        /// <summary>
        /// 通知渠道过滤
        /// </summary>
        public NotificationChannel? Channel { get; set; }

        /// <summary>
        /// 状态过滤
        /// </summary>
        public NotificationStatus? Status { get; set; }

        /// <summary>
        /// 优先级过滤
        /// </summary>
        public NotificationPriority? Priority { get; set; }

        /// <summary>
        /// 分类过滤
        /// </summary>
        public string? Category { get; set; }
    }

    /// <summary>
    /// 获取用户通知统计输入参数
    /// </summary>
    public class GetUserNotificationStatisticsInput
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public Guid UserId { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }
    }

    /// <summary>
    /// 获取批次统计输入参数
    /// </summary>
    public class GetBatchStatisticsInput
    {
        /// <summary>
        /// 发送者ID
        /// </summary>
        public Guid? SenderId { get; set; }

        /// <summary>
        /// 批次类型
        /// </summary>
        public NotificationBatchType? Type { get; set; }

        /// <summary>
        /// 批次状态
        /// </summary>
        public NotificationBatchStatus? Status { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }
    }

    /// <summary>
    /// 获取订阅统计输入参数
    /// </summary>
    public class GetSubscriptionStatisticsInput
    {
        /// <summary>
        /// 是否启用过滤
        /// </summary>
        public bool? IsEnabled { get; set; }

        /// <summary>
        /// 语言偏好过滤
        /// </summary>
        public string? LanguagePreference { get; set; }

        /// <summary>
        /// 时区偏好过滤
        /// </summary>
        public string? TimeZonePreference { get; set; }
    }

    /// <summary>
    /// 获取性能统计输入参数
    /// </summary>
    public class GetPerformanceStatisticsInput
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 通知渠道过滤
        /// </summary>
        public NotificationChannel? Channel { get; set; }

        /// <summary>
        /// 统计粒度（按小时、按天等）
        /// </summary>
        public StatisticsGranularity Granularity { get; set; } = StatisticsGranularity.Hour;
    }

    /// <summary>
    /// 获取趋势统计输入参数
    /// </summary>
    public class GetTrendStatisticsInput
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime EndTime { get; set; }

        /// <summary>
        /// 趋势类型
        /// </summary>
        public TrendType TrendType { get; set; } = TrendType.Volume;

        /// <summary>
        /// 统计粒度
        /// </summary>
        public StatisticsGranularity Granularity { get; set; } = StatisticsGranularity.Day;

        /// <summary>
        /// 通知类型过滤
        /// </summary>
        public NotificationType? Type { get; set; }

        /// <summary>
        /// 通知渠道过滤
        /// </summary>
        public NotificationChannel? Channel { get; set; }
    }

    /// <summary>
    /// 获取清理统计输入参数
    /// </summary>
    public class GetCleanupStatisticsInput
    {
        /// <summary>
        /// 清理类型
        /// </summary>
        public NotificationCleanupType CleanupType { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }
    }

    /// <summary>
    /// 实时统计DTO
    /// </summary>
    public class RealTimeStatisticsDto
    {
        /// <summary>
        /// 当前待处理通知数量
        /// </summary>
        public int PendingNotificationsCount { get; set; }

        /// <summary>
        /// 当前发送中通知数量
        /// </summary>
        public int SendingNotificationsCount { get; set; }

        /// <summary>
        /// 当前活跃批次数量
        /// </summary>
        public int ActiveBatchesCount { get; set; }

        /// <summary>
        /// 当前暂停批次数量
        /// </summary>
        public int PausedBatchesCount { get; set; }

        /// <summary>
        /// 今日发送数量
        /// </summary>
        public int TodaySentCount { get; set; }

        /// <summary>
        /// 今日送达数量
        /// </summary>
        public int TodayDeliveredCount { get; set; }

        /// <summary>
        /// 今日失败数量
        /// </summary>
        public int TodayFailedCount { get; set; }

        /// <summary>
        /// 今日成功率
        /// </summary>
        public double TodaySuccessRate { get; set; }

        /// <summary>
        /// 平均发送时间（毫秒）
        /// </summary>
        public double AverageSendTimeMs { get; set; }

        /// <summary>
        /// 当前发送速率（条/秒）
        /// </summary>
        public double CurrentSendRate { get; set; }

        /// <summary>
        /// 峰值发送速率（条/秒）
        /// </summary>
        public double PeakSendRate { get; set; }

        /// <summary>
        /// 系统负载状态
        /// </summary>
        public SystemLoadStatus LoadStatus { get; set; }

        /// <summary>
        /// 统计生成时间
        /// </summary>
        public DateTime GeneratedAt { get; set; } = DateTime.UtcNow;
    }

    /// <summary>
    /// 性能统计DTO
    /// </summary>
    public class PerformanceStatisticsDto
    {
        /// <summary>
        /// 统计时间范围
        /// </summary>
        public DateTimeRangeDto TimeRange { get; set; } = new();

        /// <summary>
        /// 总处理数量
        /// </summary>
        public long TotalProcessed { get; set; }

        /// <summary>
        /// 成功处理数量
        /// </summary>
        public long SuccessProcessed { get; set; }

        /// <summary>
        /// 失败处理数量
        /// </summary>
        public long FailedProcessed { get; set; }

        /// <summary>
        /// 平均处理时间（毫秒）
        /// </summary>
        public double AverageProcessingTimeMs { get; set; }

        /// <summary>
        /// 最大处理时间（毫秒）
        /// </summary>
        public double MaxProcessingTimeMs { get; set; }

        /// <summary>
        /// 最小处理时间（毫秒）
        /// </summary>
        public double MinProcessingTimeMs { get; set; }

        /// <summary>
        /// P95处理时间（毫秒）
        /// </summary>
        public double P95ProcessingTimeMs { get; set; }

        /// <summary>
        /// P99处理时间（毫秒）
        /// </summary>
        public double P99ProcessingTimeMs { get; set; }

        /// <summary>
        /// 平均发送速率（条/秒）
        /// </summary>
        public double AverageThroughput { get; set; }

        /// <summary>
        /// 峰值发送速率（条/秒）
        /// </summary>
        public double PeakThroughput { get; set; }

        /// <summary>
        /// 按渠道性能统计
        /// </summary>
        public Dictionary<NotificationChannel, ChannelPerformanceDto> ChannelPerformance { get; set; } = new();

        /// <summary>
        /// 按类型性能统计
        /// </summary>
        public Dictionary<NotificationType, TypePerformanceDto> TypePerformance { get; set; } = new();

        /// <summary>
        /// 错误统计
        /// </summary>
        public ErrorStatisticsDto ErrorStatistics { get; set; } = new();

        /// <summary>
        /// 统计生成时间
        /// </summary>
        public DateTime GeneratedAt { get; set; } = DateTime.UtcNow;
    }

    /// <summary>
    /// 渠道性能DTO
    /// </summary>
    public class ChannelPerformanceDto
    {
        /// <summary>
        /// 渠道
        /// </summary>
        public NotificationChannel Channel { get; set; }

        /// <summary>
        /// 处理数量
        /// </summary>
        public long ProcessedCount { get; set; }

        /// <summary>
        /// 成功数量
        /// </summary>
        public long SuccessCount { get; set; }

        /// <summary>
        /// 失败数量
        /// </summary>
        public long FailedCount { get; set; }

        /// <summary>
        /// 成功率
        /// </summary>
        public double SuccessRate { get; set; }

        /// <summary>
        /// 平均处理时间（毫秒）
        /// </summary>
        public double AverageProcessingTimeMs { get; set; }

        /// <summary>
        /// 平均发送速率（条/秒）
        /// </summary>
        public double AverageThroughput { get; set; }
    }

    /// <summary>
    /// 类型性能DTO
    /// </summary>
    public class TypePerformanceDto
    {
        /// <summary>
        /// 通知类型
        /// </summary>
        public NotificationType Type { get; set; }

        /// <summary>
        /// 处理数量
        /// </summary>
        public long ProcessedCount { get; set; }

        /// <summary>
        /// 成功数量
        /// </summary>
        public long SuccessCount { get; set; }

        /// <summary>
        /// 失败数量
        /// </summary>
        public long FailedCount { get; set; }

        /// <summary>
        /// 成功率
        /// </summary>
        public double SuccessRate { get; set; }

        /// <summary>
        /// 平均处理时间（毫秒）
        /// </summary>
        public double AverageProcessingTimeMs { get; set; }

        /// <summary>
        /// 平均发送速率（条/秒）
        /// </summary>
        public double AverageThroughput { get; set; }
    }

    /// <summary>
    /// 错误统计DTO
    /// </summary>
    public class ErrorStatisticsDto
    {
        /// <summary>
        /// 总错误数量
        /// </summary>
        public long TotalErrorCount { get; set; }

        /// <summary>
        /// 错误率
        /// </summary>
        public double ErrorRate { get; set; }

        /// <summary>
        /// 按错误类型统计
        /// </summary>
        public Dictionary<string, long> ErrorsByType { get; set; } = new();

        /// <summary>
        /// 按错误来源统计
        /// </summary>
        public Dictionary<string, long> ErrorsBySource { get; set; } = new();

        /// <summary>
        /// 最常见错误
        /// </summary>
        public List<CommonErrorDto> MostCommonErrors { get; set; } = new();
    }

    /// <summary>
    /// 常见错误DTO
    /// </summary>
    public class CommonErrorDto
    {
        /// <summary>
        /// 错误类型
        /// </summary>
        public string ErrorType { get; set; } = string.Empty;

        /// <summary>
        /// 错误消息
        /// </summary>
        public string ErrorMessage { get; set; } = string.Empty;

        /// <summary>
        /// 错误数量
        /// </summary>
        public long ErrorCount { get; set; }

        /// <summary>
        /// 占总错误的百分比
        /// </summary>
        public double Percentage { get; set; }
    }

    /// <summary>
    /// 趋势统计DTO
    /// </summary>
    public class TrendStatisticsDto
    {
        /// <summary>
        /// 趋势类型
        /// </summary>
        public TrendType TrendType { get; set; }

        /// <summary>
        /// 统计粒度
        /// </summary>
        public StatisticsGranularity Granularity { get; set; }

        /// <summary>
        /// 统计时间范围
        /// </summary>
        public DateTimeRangeDto TimeRange { get; set; } = new();

        /// <summary>
        /// 趋势数据点
        /// </summary>
        public List<TrendDataPointDto> DataPoints { get; set; } = new();

        /// <summary>
        /// 平均值
        /// </summary>
        public double Average { get; set; }

        /// <summary>
        /// 峰值
        /// </summary>
        public double Peak { get; set; }

        /// <summary>
        /// 最低值
        /// </summary>
        public double Minimum { get; set; }

        /// <summary>
        /// 峰值时间
        /// </summary>
        public DateTime? PeakTime { get; set; }

        /// <summary>
        /// 趋势方向
        /// </summary>
        public TrendDirection Direction { get; set; }

        /// <summary>
        /// 变化率
        /// </summary>
        public double ChangeRate { get; set; }

        /// <summary>
        /// 统计生成时间
        /// </summary>
        public DateTime GeneratedAt { get; set; } = DateTime.UtcNow;
    }

    /// <summary>
    /// 趋势数据点DTO
    /// </summary>
    public class TrendDataPointDto
    {
        /// <summary>
        /// 时间点
        /// </summary>
        public DateTime Time { get; set; }

        /// <summary>
        /// 数值
        /// </summary>
        public double Value { get; set; }

        /// <summary>
        /// 数量
        /// </summary>
        public long Count { get; set; }

        /// <summary>
        /// 标签
        /// </summary>
        public string? Label { get; set; }
    }

    /// <summary>
    /// 统计粒度
    /// </summary>
    public enum StatisticsGranularity
    {
        Minute = 1,
        Hour = 2,
        Day = 3,
        Week = 4,
        Month = 5,
        Quarter = 6,
        Year = 7
    }

    /// <summary>
    /// 趋势类型
    /// </summary>
    public enum TrendType
    {
        Volume = 1,
        SuccessRate = 2,
        ProcessingTime = 3,
        Throughput = 4,
        ErrorRate = 5
    }

    /// <summary>
    /// 趋势方向
    /// </summary>
    public enum TrendDirection
    {
        Up = 1,
        Down = 2,
        Stable = 3,
        Fluctuating = 4
    }

    /// <summary>
    /// 系统负载状态
    /// </summary>
    public enum SystemLoadStatus
    {
        Low = 1,
        Normal = 2,
        High = 3,
        Overload = 4
    }

    /// <summary>
    /// 通知清理类型
    /// </summary>
    public enum NotificationCleanupType
    {
        Expired = 1,
        Archived = 2,
        Failed = 3,
        Completed = 4
    }
}